       #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "sqlite3.h"
#include "logger.h"
typedef struct data
{

	char SN[64];
	char TIME[64];
	char TEMP[64];
}st_data;



int sqlite_save( sqlite3 *db, char *sql)//存入数据库	
{
	
	char          delim[] = "/";
	char         *p = NULL;	
	int           i=0 ;
	char         *data[2];
	char          insert[1024];
        char         *errmsg;
	char		  **dbresult;
	int		    row=0;
	int		    column=0;



	if( !db||!sql )
	{
		log_error("ERROR: Invalid input arguments\n");//无效的参数
		return -1;
	}

	for(p = strtok(sql, delim);p != NULL; p = strtok(NULL, delim))
        {
		data[i]=p;
		i++;

	}

	snprintf(insert, sizeof(insert), "insert into client_pj(SN, TIME, TEMP) values('%s', '%s', '%s');", data[0], data[1], data[2]); 

    //   char create[]="create table client_pj(ID integer PRIMARY KEY, SN char(10), TIME char(50), TEMP cha(10));";

	if(sqlite3_get_table(db, insert, &dbresult, &row, &column, &errmsg)!= 0)
	{
	    	log_error("%s\n", errmsg);
	}
	return 0 ;
}
int sql_create(sqlite3 *db)
{
        char create[]="create table client_pj(ID integer PRIMARY KEY, SN char(10), TIME char(50), TEMP cha(10));";
        char *errmsg;
 

        if(sqlite3_exec(db, create, NULL, NULL,&errmsg)!=0)
	{
		log_error("%s\n",errmsg);
	}
	return 0;
}


int callback_num(void *para, int num,char *value[], char *name[])//从数据库拿出来
{
	int             *p;

	p= (int *)para;//指针指向para结构体数组
       *p=atoi(value[0]);
	return 0;
}
#if 0
int callback_first(void *para, int num,char *value[], char *name[])//从数据库拿出来
{
	st_data            *p;
	int                 i;
	p= (st_data *)para+1;//指针指向para结构体数组
/*	for (i=0; i<num; i++)//num ==4
	{
		printf("%s:\t %s\n", name[i], value[i]);
	}

	snprintf(calbuf, sizeof(calbuf), "%s/%s/%s", value[1], value[2], value[3]);*/
/*value[0] = ID, 通过指针赋值改变结构体数组的值*/
	strcpy(p->SN, value[1]);
	
        strcpy(p->TIME, value[2]);

	strcpy(p->TEMP, value[3]);

	return 0;
}
#endif

int sqlite_select_one(sqlite3 *db, sqlite3_callback callback, st_data para[], int ID)//从数据库中获取数据
{
	char               *errmsg;
	char                select[128];
/*通过传递采样到的最新时间（days）参数来定位存入数据的最新的一条数据，从而select获取这条数据，最后通过回调函数callback将他存入结构体数组para*/
	snprintf(select, sizeof(select),"select * from client_pj where ID=%d;", ID);
//	printf("select:%s\n", select);

        if(sqlite3_exec(db, select, callback, (void *)para, &errmsg)!=0)//为什么传的是db的值不是地址???
        {
            log_error("%s\n", errmsg);
        }
	return 0;
}

int sqlite_select_num(sqlite3 *db, int *times)//从数据库中获取数据
{
	char               *errmsg;
	char                select[128];
/*通过传递采样到的最新时间（days）参数来定位存入数据的最新的一条数据，从而select获取这条数据，最后通过回调函数callback将他存入结构体数组para*/
	snprintf(select, sizeof(select),"select * from client_pj ;");
//	printf("select:%s\n", select);

        if(sqlite3_exec(db, select, callback_num, (void *)times, &errmsg)!=0)//为什么传的是db的值不是地址???
        {
            log_error("%s\n", errmsg);
        }
	return 0;
}


int sqlite_del(sqlite3 *db)//删除数据库
{
        char               *errmsg;
	char                delete[]="delete from client_pj limit 1;";
	char		  **dbresult;
	int		    row=0;
	int		    column=0;


        if(sqlite3_get_table(db, delete, &dbresult, &row, &column, &errmsg)!=0)//为什么传的是db的值不是地址???
        {
            log_error("%s\n", errmsg);
        }


	return 0;
}
